import pymysql

#获取数据库连接
def get_connection():
    host = 'localhost'
    port = 3306
    user = 'root'
    password = '123456'
    database = 'maoyan'
    db = pymysql.connect(host, user, password, database, charset='utf8', port=port)
    return db

#获取游标
def get_cursor(con):
    cursor = con.cursor()
    return cursor


#关闭数据库连接
def close_connecion(con):
    con.close()

#插入数据
def save_db(con,cursor,data_dict):
    sql = 'insert into movie (title,releasetime,actor,ranks,score,cover) values ("%s","%s","%s","%s","%s","%s")' % (data_dict['title'],data_dict['releasetime'],data_dict['actor'],data_dict['rank'],data_dict['score'],data_dict['cover'])
    print(sql)
    cursor.execute(sql)
    con.commit()

# 可以避免一定的sql注入问题
# insert into movie(title, actor, release_time, cover) values("Qadir"; drop database maoyan9;", "b", "c", "d");
def save_db2(con,cursor,data_dict):
    sql = 'insert into movie (title,releasetime,actor,ranks,score,cover) values (%s,%s,%s,%s,%s,%s)'
    print(sql)
    cursor.execute(sql,(data_dict['title'],data_dict['releasetime'],data_dict['actor'],data_dict['rank'],data_dict['score'],data_dict['cover']))
    con.commit()
